package pdm.traj.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import pdm.traj.data.TrackPoint;
import pdm.traj.entity.TrackPoint2;
import pdm.traj.entity.TrajQuery;

public class TrackPointUtil {
	public static final SimpleDateFormat sdf = new SimpleDateFormat(TrajConstants.TIMESTAMP_FORMAT_STR);
	public static void assembleTrackPoint(String key, String valueStr, TrackPoint2 tp) {
		TrackPointUtil.modifyTPByKey(key, tp);
		TrackPointUtil.modifyTPByEntryValue(valueStr, tp);
	}
	
	/**
	 * complete class members of a TrackPoint2 object with 
	 * information stored in key
	 * @param key
	 * @param tp
	 */
	public static void modifyTPByKey(String key, TrackPoint2 tp) {
//		StringTokenizer st = new StringTokenizer(key, "_", false);
		String[] strs = key.split("_");
		String lacid = null, cellid = null;
		lacid = strs[3];
		cellid = strs[4];
		tp.setLacid(lacid);
		tp.setCellid(cellid);
	}
	
//	public static void modifyTPByKey(String key, TrackPoint2 tp) {
//		StringTokenizer st = new StringTokenizer(key, "_", false);
//		String lacid = null, cellid = null;
//		if (st.hasMoreTokens()) {
//			lacid = st.nextToken();
//		} else {
//			return;
//		}
//		if (st.hasMoreTokens()) {
//			cellid = st.nextToken();
//		} else {
//			return;
//		}
//		tp.setLacid(lacid);
//		tp.setCellid(cellid);
//	}
	
	public static void modifyTPByEntryValue(String value, TrackPoint2 tp) {
		StringTokenizer st = new StringTokenizer(value, ",", false);
		String uidStr = null, tsStr = null;
		if (st.hasMoreTokens()) {
			uidStr = st.nextToken();
		} else {
			return;
		}
		if (st.hasMoreTokens()) {
			tsStr = st.nextToken();
		} else {
			return;
		}
		tp.setId(uidStr);
		TrackPointUtil.setTPDateTime(tsStr, tp);
	}
	
	public static int setTPDateTime(String dt, TrackPoint tp) {
		StringTokenizer st = new StringTokenizer(dt, " ", false);
		String date = null, time = null;
		if (st.hasMoreTokens()) {
			date = st.nextToken();
		} else {
			return 0;
		}
		if (st.hasMoreTokens()) {
			time = st.nextToken();
		} else {
			return 0;
		}
		tp.setDate(date);
		tp.setTime(time);
		return 1;
	}
	
	public static String getTimestamp(TrackPoint2 tp) {
		if (tp != null) {
			return tp.getDate() + " " + tp.getTime();
		} else {
			return "";
		}
	} 
	
	public static String getSid(TrackPoint2 tp) {
		if (tp != null) {
			return tp.getLacid() + " " + tp.getCellid(); 
		} else {
			return "";
		}
	}
	
	public static Date getStartTime(TrajQuery query) {
		String str = query.getStartTime();
		if (str == null) {
			return null;
		} else {
			try {
				Date d = sdf.parse(str);
				return d;
			} catch (ParseException e) {
//				e.printStackTrace();
				return null;
			}
		}
	}
	
	public static Date getEndTime(TrajQuery query) {
		String str = query.getEndTime();
		if (str == null) {
			return null;
		} else {
			try {
				Date d = sdf.parse(str);
				return d;
			} catch (ParseException e) {
//				e.printStackTrace();
				return null;
			}
		}
	}
	
	public static Date getTs(TrackPoint2 tp) {
		if (tp == null) {
			return null;
		} else {
			try {
				Date d = sdf.parse(getTimestamp(tp));
				return d;
			} catch (ParseException e) {
//				e.printStackTrace();
				return null;
			}
		}
	}
	
	/**
	 * 判断tp是否符合query查询条件
	 * @param tp
	 * @param query
	 * @return
	 */
//	public static boolean isGoodTP(TrackPoint2 tp, TrajQuery query) {
//		String tsStr = getTimestamp(tp);
//		Date ts;
//		try {
//			ts = sdf.parse(tsStr);
//			if (query.getStartTime())
//		} catch (ParseException e) {
////			e.printStackTrace();
//			return false;
//		} 
//		
//	}
	/**
	 * 获得一个sid中的Lacid（主id）
	 * @param sid
	 * @return
	 */
	public static String getLacidFromSid(String sid) {
		String[] strs = sid.split("_");
		if (strs.length == 2) {
			return strs[0];
		} else {
			return sid;
		}
	}
	
	public static void printTPList(List<TrackPoint2> tpl) {
		if (tpl != null) {
			for (TrackPoint2 tp : tpl) {
				System.out.println(tp);
			}
		}
	}
}
